<?php

########################################################################################
# Empresa :     Tecnopac Informatica;                                                  #
# Autor:        Deiverson Pacheco;                                                     #
# Data Criação: 18/12/2010;                                                            #
########################################################################################
session_start();
header("Cache-control: private");
include_once("../../includes/session.php");


//Linha para formatação de caracteres com acentuação
header("Content-Type: text/html; charset=ISO-8859-1",true);

//======================================================================================
//==> Include obrigatórias no script
//======================================================================================
include_once('../../includes/classes/class.TemplatePower.php');
include_once('../../includes/classes/class.Conexao.php');
include_once('../../includes/classes/class.Geral.php');

//======================================================================================
//==> Inicializar Classes
//======================================================================================
$not  = new Persist;
$not2  = new Persist;
$data = new getData;
$cor  = new ColorTr;
$util = new Util;
$vld  = new ValidarDados;
$sql  = new SqlInjection;
//===================================================================================================

$id  = @$sql->tratar($_GET['id'], 30);

//incluindo o arquivo do fpdf
include_once('../../includes/classes/fpdf/class.Fpdf.php');

//defininfo a fonte !
define('FPDF_FONTPATH','../../includes/classes/fpdf/font/');

//instancia a classe.. P=Retrato, mm =tipo de medida utilizada no casso milimetros, tipo de folha =A4

$pdf = new FPDF("P","mm","A4");		
$pdf->SetTopMargin(15);
$pdf->SetLeftMargin(8);
$pdf->SetRightMargin(5);

//posicao vertical no caso -1.. e o limite da margem
//$pdf->SetY("-1");

$pessoa		 		= @trim(strtoupper($_GET['pessoa']));
$idcategoria 		= @trim(strtoupper($_GET['idcategoria']));
//$relatorio	 		= @trim(strtoupper($_GET['relatorio']));
extract($_GET);	

//$data_ini = $data->getDataFormatada($dtinicial);
//$data_fim = $data->getDataFormatada($dtfinal);


	$titulo = utf8_decode('Relatório Analítico de Contatos');
	



//define o TOP MARGIN


//---------------------------------------------------------------------------------------------		
// SQL
//---------------------------------------------------------------------------------------------		

		
	extract($_GET);	
	$not->sql = " select cadastro.id as id_cadastro, cadastro.pessoa, case cadastro.pessoa when 1 then 'Pessoa Física' when 2 then 'Pessoa Jurídica' end as tp_pessoa,
				cadastro.nome_razao, cadastro.doc as cpf_cnpj, cadastro.dt_nascimento,
				cadastro.rg, cadastro.residencial, cadastro.comercial, cadastro.celular, cadastro.fax, cadastro.cep,
				cadastro.endereco as logradouro, cadastro.numero, cadastro.complemento, cadastro.bairro, cadastro.cidade,
				cadastro.uf, cadastro.email, cadastro.website, 
				categorias_associadas.id_cat, categorias.id, categorias.categoria
				from cadastro
				inner join categorias_associadas on cadastro.id = categorias_associadas.id_cadastro
				inner join categorias on categorias_associadas.id_cat = categorias.id
				where cadastro.ativo = 'Sim' ";
				
	if($pessoa != ""){
		$not->sql .= " and cadastro.pessoa = ".$pessoa." ";
	}
	if($idcategoria != ""){
		$not->sql .= " and categorias.id = ".$idcategoria." ";
	}

	$not->sql .= " group by cadastro.id ";	
//---------------------------------------------------------------------------------------------		
// PROCESSOS
//---------------------------------------------------------------------------------------------			
	if($not->ler()>0){					
		while($not->ini()){	
	
			$tp_pessoa 		   	   = $not->tp_pessoa;
			$nome_razao			   = $not->nome_razao;
			$dt_nascimento  	   = $data->getDataFormatada($not->dt_nascimento);			
			$cpf_cnpj		  	   = $not->cpf_cnpj;
			$rg			  		   = $not->rg;	
			$residencial	  	   = $not->residencial;
			$comercial	  		   = $not->comercial;	
			$celular		  	   = $not->celular;
			$fax		  		   = $not->fax;				
			$logradouro		  	   = $not->logradouro;
			$cep		  		   = $not->cep;							
			$numero		  		   = $not->numero;		
			$complemento  		   = $not->complemento;				
			$bairro			  	   = $not->bairro;
			$cidade		  		   = $not->cidade;							
			$uf			  		   = $not->uf;							
			$email		  		   = $not->email;							
			$website	  		   = $not->website;								



	$pdf->addPage();
	
	$pdf->SetFont('arial','',10);
	$pdf->Cell(180,1,'','B',1,'C');
	$pdf->Ln(0);	
	
	$pdf->SetFont('arial','B',12);
	$pdf->Cell(180,5,$titulo,0,1,'C');
		
	$pdf->SetFont('arial','',10);
	$pdf->Cell(180,1,'','T',1,'C');
	$pdf->Ln(1);

//---------------------------------------------------------	
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Tipo de Pessoa: ')),0,0,'L');
			$pdf->Ln(0);

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,utf8_decode($tp_pessoa),0,1,'L');
			$pdf->Ln(1);			
				
			//---------------------------------------	
				
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Nome / Razão Social: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$nome_razao,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('Nascimento: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$dt_nascimento,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------
			
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('CNPJ / CPF: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$cpf_cnpj,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('RG: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$rg,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------	
			
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Tel. Residencial: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$residencial,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('Tel. Comercial: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$comercial,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------		
			
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Celular: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$celular,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('Fax: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$fax,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------	
			
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Logradouro: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$logradouro,0,1,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('CEP: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$cep,0,0,'L');
			$pdf->Ln(0);					
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('Nr: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$numero,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------					

			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Complemento: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$complemento,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('Bairro: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$bairro,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------	
			
			
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('Cidade: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$cidade,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('UF: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$uf,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------		
			
						
			$pdf->SetX(10);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(10,5,(utf8_decode('E-mail: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(45);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(45,5,$email,0,0,'L');
			$pdf->Ln(0);		
			
			
			
			$pdf->SetX(115);
			$pdf->SetFont('arial','',9);
			$pdf->Cell(115,5,(utf8_decode('Site: ')),0,0,'L');
			$pdf->Ln(0);
			

			$pdf->SetX(140);
			$pdf->SetFont('arial','',8);
			$pdf->Cell(140,5,$website,0,1,'L');
			$pdf->Ln(1);							

			//---------------------------------------								
						
			$pdf->SetFont('arial','',10);
			$pdf->Cell(180,1,'','T',1,'C');
			$pdf->Ln(7);



			//---------------------------------------------------------------------------------------------		
			// CATEGORIAS
			//---------------------------------------------------------------------------------------------		
			
				extract($_GET);	
				$not2->sql = " select cadastro.id, 
							categorias_associadas.id_cat, categorias.id, categorias.categoria
							from cadastro
							inner join categorias_associadas on cadastro.id = categorias_associadas.id_cadastro
							inner join categorias on categorias_associadas.id_cat = categorias.id
							where cadastro.ativo = 'Sim' and cadastro.id = ".$not->id_cadastro." ";
							
			
					$pdf->SetFont('arial','',10);
					$pdf->Cell(180,1,'','B',1,'C');	
					
					$pdf->SetFont('arial','B',12);
					$pdf->Cell(180,5,(utf8_decode('Categoria')),0,1,'C');
					$pdf->Ln(0);


					$pdf->SetFont('arial','',10);
					$pdf->Cell(180,1,'','T',1,'C');	
					$pdf->Ln(1);				

					$pdf->SetX(10);
					$pdf->SetFont('arial','',9);
					$pdf->Cell(10,3,(utf8_decode('Nome Categoria')),0,1,'L');
					$pdf->Ln(0);					

					$pdf->SetFont('arial','',10);
					$pdf->Cell(180,1,'','B',1,'C');
						
				
					if($not2->ler()>0){					
						while($not2->ini()){	
					
							$categoria 		   	   = $not2->categoria;
				
							$pdf->SetX(10);
							$pdf->SetFont('arial','',8);
							$pdf->Cell(10,5,$categoria,0,1,'L');
							$pdf->Ln(0);				
				
						}
					}else{
					$pdf->SetFont('arial','B',12);
					$pdf->Cell(180,5,(utf8_decode('Nenhum dado foi encontrado.')),0,1,'C');			
	}
			$pdf->SetFont('arial','',10);
			$pdf->Cell(180,3,'','T',1,'C');

		}
	
	}else{
		$pdf->SetFont('arial','B',12);
		$pdf->Cell(180,5,(utf8_decode('Nenhum dado foi encontrado.')),0,1,'C');			
	}
	if($cont == ""){
		$cont = 0;	
	}
	$pdf->Ln(2);		
	
	
	

	/*$pdf->SetFont('arial','B',10);
	$pdf->Cell(180,5,utf8_decode('Número de registros listados: ').$cont,'T',1,'C');
	$pdf->Ln(1);		
	
	$pdf->SetFont('arial','',10);
	$pdf->Cell(180,3,'','T',1,'C');*/
	
		
	$pdf->Output("relatorio_analitico.pdf","D");
?>